package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.db.Authentication;
import com.apiit.webserviceclient.db.Authentication_DAO;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LecturerIndex extends Activity implements OnClickListener {

	private Authentication_DAO datasource;

	Button library, pagol, timetable, leave;
	TextView logout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lecturer_index);

		library = (Button) findViewById(R.id.button2);

		pagol = (Button) findViewById(R.id.Pagol);
		timetable = (Button) findViewById(R.id.timetable);

		leave = (Button) findViewById(R.id.leave);
		logout = (TextView) findViewById(R.id.txtlogout);

		library.setOnClickListener(this);

		pagol.setOnClickListener(this);
		timetable.setOnClickListener(this);
		leave.setOnClickListener(this);

		logout.setOnClickListener(this);

		datasource = new Authentication_DAO(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lecturer_index, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == library) {
			datasource.open();
			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_lib_perUser);
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, Login_Activity.class);
				startActivity(i);
			} else {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/gims/library/Logon.asp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"SID",
						"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;
				try {
					respones = bpelProcess.doRequest(request);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk/gims/library/Logon.asp&SID="
						+ respones.getResultsMap().get("SID") + "&APPID=";
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("title", "//title---");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();

				parametersMap2
						.put("txtUserName", authentication.getuser_name());
				parametersMap2.put("txtPassword", authentication.getpassword());
				parametersMap2.put("REDIRECTPAGE",
						"http://webspace1.apiit.lk/gims/library/Logon.asp");
				parametersMap2.put("SID", respones.getResultsMap().get("SID"));
				parametersMap2.put("APPID", "");
				parametersMap2.put("Submit", "Login");

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;
				try {
					respones2 = bpelProcess.doRequest(request2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Process bpelProcess3 = new BPEL_Process();
				BPEL_Request request3 = new BPEL_Request();
				request3.setUrl("http://webspace1.apiit.lk/gims/library/Links.asp");

				Map<String, String> xpathMap3 = new HashMap<String, String>();
				xpathMap3.put("title", "/html[1]/body[1]/p[4]/a[1]---");
				request3.setXpathMap(xpathMap3);
				Map<String, String> parametersMap3 = new HashMap<String, String>();
				request3.setParametersMap(parametersMap3);
				request3.setType("GET");
				BPEL_Response respones3 = null;

				try {
					respones3 = bpelProcess3.doRequest(request3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (respones3.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String testResult = respones3.getResultsMap().get("title");

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, Library.class);
					i.putExtra("title", testResult);
					startActivity(i);
				} else {

					AlertDialog.Builder alt = new AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, Login_Activity.class);
					startActivity(i);
				}
			}
			datasource.close();
		}

		if (v == pagol) {
			// Link---- http://webspace1.apiit.lk/pagol15/www/
			datasource.open();
			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_pagolLec_perUser);
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, PagolLecturerLogin.class);
				// i.putExtra("", respones.getResultsMap().get("name"));
				startActivity(i);
			} else {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/pagol15/www/login.asp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"SID",
						"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;
				try {
					respones = bpelProcess.doRequest(request);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk:80/pagol15/www/login.asp&SID="
						+ respones.getResultsMap().get("SID");
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("title", "//title---");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();

				parametersMap2
						.put("txtUserName", authentication.getuser_name());
				parametersMap2.put("txtPassword", authentication.getpassword());
				parametersMap2.put("REDIRECTPAGE",
						"http://webspace1.apiit.lk:80/pagol15/www/login.asp");
				parametersMap2.put("SID", respones.getResultsMap().get("SID"));
				parametersMap2.put("APPID", "");
				parametersMap2.put("Submit", "Login");

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;
				try {
					respones2 = bpelProcess.doRequest(request2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Process bpelProcess3 = new BPEL_Process();
				BPEL_Request request3 = new BPEL_Request();
				request3.setUrl("http://webspace1.apiit.lk/pagol15/www/lecturer_top.asp");

				Map<String, String> xpathMap3 = new HashMap<String, String>();
				xpathMap3
						.put("title",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[2]/td[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[2]---");
				request3.setXpathMap(xpathMap3);
				Map<String, String> parametersMap3 = new HashMap<String, String>();
				request3.setParametersMap(parametersMap3);
				request3.setType("GET");
				BPEL_Response respones3 = null;

				try {
					respones3 = bpelProcess3.doRequest(request3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (respones3.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String testResult = respones3.getResultsMap().get("title");

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, LecturerPagol.class);
					i.putExtra("title", testResult);
					startActivity(i);
				} else {

					AlertDialog.Builder alt = new AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, PagolLecturerLogin.class);
					startActivity(i);
				}
			}
			datasource.close();
		}

		if (v == leave) {

			datasource.open();
			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_applyLeave_perUser);
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, ApplyLeaveLogin.class);
				startActivity(i);
			} else {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/eHRM-Web/LoginPage.aspx");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put("test",
						"/html[1]/body[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[2]/a[6]---");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				parametersMap.put("__VIEWSTATE",
						"%2FwEPDwULLTE4MTc4MjA1NzFkZA%3D%3D");
				parametersMap
						.put("__EVENTVALIDATION",
								"%2FwEWBgK8rOK2AgKPpuq2CALyveCRDwKPyPGACgLfndyKBAL5i%2BPNBg%3D%3D");
				parametersMap.put("userName", authentication.getuser_name());
				parametersMap.put("password", authentication.getpassword());
				parametersMap.put("login", "Login");
				parametersMap.put("password", "statistics");
				parametersMap.put("login", "Login");
				parametersMap.put("FooterBarUI1%3AscreenTopHidden", "0");
				parametersMap.put("FooterBarUI1%3AscreenLeftHidden", "0");
				request.setParametersMap(parametersMap);
				request.setType("POST");
				BPEL_Response respones = null;
				try {
					respones = bpelProcess.doRequest(request);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (respones.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String testResult = respones.getResultsMap().get("title");

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, Library.class);
					i.putExtra("title", testResult);
					startActivity(i);
				} else {
					AlertDialog.Builder alt = new AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, ApplyLeaveLogin.class);
					startActivity(i);
				}
			}
			datasource.close();
		}
		if (v == timetable) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://timetable.apiit.lk/TT-Version17.0/");

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("text",
					"//input[@name='javax.faces.ViewState']---value");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (respones.getStatus().equalsIgnoreCase(
					GlobalData.message_success)) {
				String testResult = respones.getResultsMap().get("text");

				Toast.makeText(getApplicationContext(), testResult,
						Toast.LENGTH_LONG).show();
				Intent i = new Intent(this, TimeTable.class);
				i.putExtra("text", testResult);
				startActivity(i);

			} else {

				Intent i = new Intent(this, Index.class);
				startActivity(i);
			}

		}
		if (v == logout) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace.apiit.lk/myspace.jsp?Action=LogOut");

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put(
					"text",
					"/html[1]/body[1]/div[1]/div[1]/div[3]/ul[1]/li[2]/li[1]/li[3]/div[1]/ul[1]/li[1]/a[1]/span[1]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (respones.getStatus().equalsIgnoreCase(
					GlobalData.message_success)) {
				String testResult = respones.getResultsMap().get("text");
				GlobalData.cookies = "";
				Toast.makeText(getApplicationContext(), testResult,
						Toast.LENGTH_LONG).show();

				Intent i = new Intent(this, MainActivity.class);
				i.putExtra("text", testResult);
				startActivity(i);
			} else {

				Intent i = new Intent(this, Index.class);
				startActivity(i);
			}
		}

	}

}
